/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import AdminOperations.AdminManageCategory;
import AdminOperations.AdminManageForum;
import CRUD.CategoryHome;
import Utility.Utility;
import beans.AdminCategoryBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xmlMapping.Category;

/**
 *
 * @author Roaa
 */
public class AdminCategoryAction extends org.apache.struts.actions.DispatchAction {

    /* forward name="success" path="" */
    //private static final String SUCCESS = "success";
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.m 
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward loadCategories(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ((AdminCategoryBean) form).setCategories(new Utility().getCategories());
        return mapping.findForward("adminCategory");
    }

    public ActionForward deleteCategories(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String[] sel = ((AdminCategoryBean) form).getSelected();
        AdminManageCategory manageCat = new AdminManageCategory();
        for (int i = 0; i < sel.length; i++) {
            manageCat.removeCategory(Integer.parseInt(sel[i]));
        }

        ((AdminCategoryBean) form).setCategories(new Utility().getCategories());
        return mapping.findForward("adminCategory");
    }

    public ActionForward editCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int catId = Integer.parseInt(request.getParameter("catId"));
        ((AdminCategoryBean) form).setCategory(null);
        Category selectedCat = new CategoryHome().findById(catId);
        ((AdminCategoryBean) form).setCategory(selectedCat);
        ((AdminCategoryBean) form).setDesc("");
        ((AdminCategoryBean) form).setName("");
        /*((AdminCategoryBean) form).setForums(new Utility().getForums(catId));
        ((AdminCategoryBean) form).setName(selectedCat.getName());
        ((AdminCategoryBean) form).setDesc(selectedCat.getDescription());*/
        return mapping.findForward("admEditCategory");
    }

    public ActionForward addCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String name = ((AdminCategoryBean) form).getName();
        String desc = ((AdminCategoryBean) form).getDesc();
        AdminManageCategory manageCat = new AdminManageCategory();
        Category newCategory = new Category(name, desc);
        manageCat.addCategory(newCategory);
        ((AdminCategoryBean) form).setCategories(new Utility().getCategories());
        return mapping.findForward("adminCategory");
    }

    public ActionForward deleteForums(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String[] sel = ((AdminCategoryBean) form).getSelected();
        AdminManageForum manageCat = new AdminManageForum();
        for (int i = 0; i < sel.length; i++) {
            manageCat.deleteForum(Integer.parseInt(sel[i]));
        }
        int catId = ((AdminCategoryBean)form).getCatId();
        ((AdminCategoryBean) form).setCategory(new CategoryHome().findById(catId));
        return mapping.findForward("admEditCategory");
    }

    public ActionForward saveCatChanges(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String name = ((AdminCategoryBean) form).getName();
        String desc = ((AdminCategoryBean) form).getDesc();
        int id = ((AdminCategoryBean) form).getCatId();
        AdminManageCategory manageCat = new AdminManageCategory();
        Category newCategory = new Category(name, desc);
        newCategory.setCategoryId(id);
        manageCat.renameCategory(newCategory);
        ((AdminCategoryBean) form).setCategories(new Utility().getCategories());
        return mapping.findForward("adminCategory");
    }
}
